package cn.iocoder.yudao.module.bpm.controller.admin.formtype;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bpm.controller.admin.formtype.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.formtype.FormTypeDO;
import cn.iocoder.yudao.module.bpm.convert.formtype.FormTypeConvert;
import cn.iocoder.yudao.module.bpm.service.formtype.FormTypeService;

@Api(tags = "a管理后台 - 表单类型")
@RestController
@RequestMapping("/bpm/form-type")
@Validated
public class FormTypeController {

    @Resource
    private FormTypeService formTypeService;

    @PostMapping("/create")
    @ApiOperation("创建表单类型")
    @PreAuthorize("@ss.hasPermission('bpm:form-type:create')")
    public CommonResult<Long> createFormType(@Valid @RequestBody FormTypeCreateReqVO createReqVO) {
        return success(formTypeService.createFormType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新表单类型")
    @PreAuthorize("@ss.hasPermission('bpm:form-type:update')")
    public CommonResult<Boolean> updateFormType(@Valid @RequestBody FormTypeUpdateReqVO updateReqVO) {
        formTypeService.updateFormType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除表单类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('bpm:form-type:delete')")
    public CommonResult<Boolean> deleteFormType(@RequestParam("id") Long id) {
        formTypeService.deleteFormType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得表单类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('bpm:form-type:query')")
    public CommonResult<FormTypeRespVO> getFormType(@RequestParam("id") Long id) {
        FormTypeDO formType = formTypeService.getFormType(id);
        return success(FormTypeConvert.INSTANCE.convert(formType));
    }

    @GetMapping("/list")
    @ApiOperation("获得表单类型列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('bpm:form-type:query')")
    public CommonResult<List<FormTypeRespVO>> getFormTypeList(@RequestParam("ids") Collection<Long> ids) {
        List<FormTypeDO> list = formTypeService.getFormTypeList(ids);
        return success(FormTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得表单类型分页")
    @PreAuthorize("@ss.hasPermission('bpm:form-type:query')")
    public CommonResult<PageResult<FormTypeRespVO>> getFormTypePage(@Valid FormTypePageReqVO pageVO) {
        PageResult<FormTypeDO> pageResult = formTypeService.getFormTypePage(pageVO);
        return success(FormTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出表单类型 Excel")
    @PreAuthorize("@ss.hasPermission('bpm:form-type:export')")
    @OperateLog(type = EXPORT)
    public void exportFormTypeExcel(@Valid FormTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FormTypeDO> list = formTypeService.getFormTypeList(exportReqVO);
        // 导出 Excel
        List<FormTypeExcelVO> datas = FormTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "表单类型.xls", "数据", FormTypeExcelVO.class, datas);
    }

}
